import json
import requests

'''
new_dct = {
    "question": "总共有多少参赛选手参加23届冬奥会瑞典代表团？",
    "answer": "116人",
    "Q": "xxxxxxx",
    "answerable": "Y",
    "candidates": [
        {
            "question": "xxxxxxx",
            "judgeScore": "Y",
        },
        {
            "question": "xxxxxxx",
            "judgeScore": "X",
        },
        {
            "question": "xxxxxxx",
            "judgeScore": "?",
        }
    ]
}
'''
url = "http://139.196.176.146:8080/api/dayu/qa?question=%s&index=oly"

target = r'data\lzf.100.json'
output =  r'data\lzf.100.标注.jsonl'

def get(url):
    req = requests.get(url)
    return req.json()


with open(target, "r",encoding='utf8') as f, \
    open(output, "a+",encoding='utf8') as o:
    for index, line in enumerate(f):
        if not line or line=="\n": continue
        line_js = json.loads(line)
        ori_question = line_js['question']
        print("=======================%d======================="%(index+1))
        print("[原始问题:]", ori_question)
        print("[原始答案:]",line_js['answer'])
        line_js['Q'] = input("输入复述：")
        line_js['answerable'] = input("能否判定结果正确与否 Y or N：")
        line_js["candidates"] = []
        tgt_url = url%(line_js['Q'])
        candidates = get(tgt_url)["candidates"][:3]
        if not len(candidates):
            print("你的问题超纲了！！")
        else:
            print("开始处理复述。。。。")

        for i, crawl_dct in enumerate(candidates):
            print("[top%d 返回问题] %s"%(i, crawl_dct["question"]))
            print("[top%d 返回答案] %s"%(i, crawl_dct["answer"]))
            is_true = input("判断返回是否正确（@|""）：")
            while is_true not in ("@", ""):
                print("输入数据不在（@|""）之间")
                is_true = input("判断返回是否正确(@|""）：")

            line_js["candidates"].append({
                "question": crawl_dct["question"],
                "answer": crawl_dct["answer"],
                "judgeScore": is_true,
            })

        # lines[index] = json.dumps(line_js, ensure_ascii=False)
        o.write(json.dumps(line_js, ensure_ascii=False) + "\n")
        o.flush()
    # for line in lines:
    #     o.write(line+'\n')


#
# f = open(output, 'w', encoding='utf-8')
# for line in lines:
#     f.write(line+'\n')


